package com.imge;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import javax.imageio.ImageIO;

public class ImgaeConverterUtil {

    // JGP格
    public static final String JPG = "jpeg";
    // GIF格式
    public static final String GIF = "gif";
    // PNG格式
    public static final String PNG = "png";
    // BMP格式
    public static final String BMP = "bmp";


    public static void converter(File imgFile, String format, File formatFile) throws IOException {
        BufferedImage bIMG = ImageIO.read(imgFile);
        ImageIO.write(bIMG, format, formatFile);
    }


    public static void main(String[] args) {
        try {
//            String urlStr = "https://s3-ap-southeast-1.amazonaws.com/public.photo138.com/gateway/test/image/0FAD17EB3F2E3B06E3E6911A0CA35D09_1598671687010.jpg";
//            boolean isExist = false;
//            try{
//                // 注：urlStr中需将空格替换为%20,否则报505
//                URL url = new URL(urlStr);
//                HttpURLConnection conn = (HttpURLConnection ) url.openConnection();
//                int state = conn.getResponseCode();
//                InputStream inputStream = conn.getInputStream();
//                final BufferedImage read = ImageIO.read(inputStream);
//                ImageIO.write(read, "png", new File("E:\\temp\\psb2.png"));
//                if(state == 200){
//                    isExist  = true;
//                }else{
//                    isExist  = false;
//                }
//            }catch(Exception e) {
//                isExist = false;
//            }
//            System.out.println(isExist);
////            // 转换为JPG
////            ImgaeConverterUtil.converter(new File("https://s3-ap-southeast-1.amazonaws.com/public.photo138.com/gateway/test/image/0FAD17EB3F2E3B06E3E6911A0CA35D09_1598671687010.jpg"), JPG, new File("E:\\temp\\psb2.jpg"));
////            // 转换为GIF
////            ImgaeConverterUtil.converter(new File("https://s3-ap-southeast-1.amazonaws.com/public.photo138.com/gateway/test/image/0FAD17EB3F2E3B06E3E6911A0CA35D09_1598671687010.jpg"), GIF, new File("E:\\temp\\psb2.gif"));
////            // 转换为PNG
////            ImgaeConverterUtil.converter(new File("https://s3-ap-southeast-1.amazonaws.com/public.photo138.com/gateway/test/image/0FAD17EB3F2E3B06E3E6911A0CA35D09_1598671687010.jpg"), PNG, new File("E:\\temp\\psb2.png"));
////            // 转换为BMP
//            int i= 1/0;
            ImgaeConverterUtil.converter(new File("E:\\temp\\icon.gif"), PNG, new File("E:\\temp\\psb2.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
